//
//  RegisteredViewController.m
//  mhome
//
//  Created by YaSha_Tom on 2018/12/1.
//  Copyright © 2018年 刘春雷. All rights reserved.
//

#import "RegisteredViewController.h"
#import "RegisteredView.h"
#import "UIButton+CountDown.h"
#import "NextViewController.h"
#import "Tools.h"

@interface RegisteredViewController ()
@property (nonatomic,strong) RegisteredView *registeredView;
@property (nonatomic,strong) NSMutableDictionary *parameter;
@property (nonatomic,strong) NSString *verificationCodeStr;
@property (nonatomic,strong) NSString *verificationCode;
@end

@implementation RegisteredViewController

- (NSMutableDictionary *)parameter {
    if(!_parameter){
        _parameter = [NSMutableDictionary dictionary];
    }
    return _parameter;
}
- (RegisteredView *)registeredView {
    if (!_registeredView) {
        _registeredView = [[RegisteredView alloc]init];
    }
    return _registeredView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.registeredView];
   @weakify(self);
    [[_registeredView.getCaptchaButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *getCaptchaButton) {
        @strongify(self);
        [getCaptchaButton startWithTime:60 title:@"重新获取" countDownTitle:@"s" mainColor:[UIColor lightGrayColor] countColor:[UIColor grayColor]];
        [self getCaptcha];
    }];
    [[_registeredView.nextButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        if([self.verificationCode isEqualToString:self.verificationCodeStr]){
            NextViewController *nextViewController = [[NextViewController alloc]init];
            [self.navigationController pushViewController:nextViewController animated:YES];
        }else{
            [QMUITips showInfo:@"请输入正确的验证码" inView:self.view hideAfterDelay:1];
        }
    }];
    [self monitorAccount];
}
- (void)monitorAccount {
    @weakify(self);
    RACSignal *validPhoneSignal = [_registeredView.phoneTextField.rac_textSignal map:^id(NSString *value) {
        @strongify(self);
        [self.parameter setValue:value forKey:@"tel"];
        return @(value.length > 0);
    }];
    RACSignal *verificationSignal = [_registeredView.captchaTextField.rac_textSignal map:^id(NSString *value) {
        @strongify(self);
        self.verificationCode = value;
        return @(value.length >0);
    }];
    RACSignal *nextButtonSignal = [RACSignal combineLatest:@[validPhoneSignal,verificationSignal] reduce:^id(NSNumber *phoneValid, NSNumber *verificationValid){
        return @([phoneValid boolValue] && [verificationValid boolValue]);
    }];
    RAC(_registeredView.getCaptchaButton, backgroundColor) = [nextButtonSignal map:^id(id value) {
        @strongify(self);
        if ([value boolValue]) {
            self.registeredView.getCaptchaButton.enabled = YES;
            return [UIColor lightGrayColor];
        } else {
            self.registeredView.getCaptchaButton.enabled = NO;
            return [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0];
        }
    }];
}
- (void)getCaptcha {
    if(![Tools checkTelNumber:self.parameter[@"tel"]]){
        [QMUITips showInfo:@"请输入正确的手机号" inView:self.view hideAfterDelay:1];
    }else{
        int k = 0;
        for ( int i = 0; i < 4; i++) {
            int j = arc4random()%9;
            k = k*10 +j;
        }
        self.verificationCodeStr = [NSString stringWithFormat:@"%d",k];//将随机4位数转换为字符串
        NSString *urlString = [NSString stringWithFormat:@"%@%@",Domain,sendSmsApi];
        [self.parameter setValue:self.verificationCodeStr forKey:@"code"];
        [self.parameter setValue:@"4e6a7f9b21ccb2b4897a1b3b1edb8abc" forKey:@"key"];
        [[NetworkManager manager] sendPostRequestWithUrl:urlString paramenters:self.parameter success:^(id responseObject) {
            NSLog(@"成功%@",responseObject);
        } failure:^(NSError *error) {
            NSLog(@"失败%@",error);
        }];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
